package functions.ems_base_data;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.EmployeeBankAccount;
import com.beans.VendorBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import coms.JSONToBean;
import coms.JSONUtil;
import frame.HttpHelper;


public class Vendor extends BaseCase{
	private org.slf4j.Logger logger = LoggerFactory.getLogger(Vendor.class);
	
	VendorBean vendor;
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public VendorBean getVendor() {
		return vendor;
	}
	public void setVendor(VendorBean vendor) {
		this.vendor = vendor;
	}
	
	/**
	 * 查询员工供应商列表
	 * vendorType = EMPLOYEE
	 * findPagedVendorListUrl="/ems-base-data/vendor/findPagedVendorList"
	 */
	public void findPagedVendorList(String type) {
		logger.info(" 查询员工供应商列表 ------- start -------------------");
		String url =Constants.FIND_PAGED_VENDOR_LIST_URL;
		String jsonString = "{\"page\":{\"currentPage\":\"1\",\"pageSize\":10,\"recordCount\":0},"
				+ "\"params\":{\"vendorCode\":\"\",\"vendorType\":\""+type + "\"},\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
    	try {
    		result = HttpHelper.post(jsonString, url);		
    		logger.info("findPagedVendorList ---" + result);
			JSONObject object = JSONUtil.toJSON(result);
			int total = object.getInt("total");
			if(total>0) {
				try {
				JSONArray array = object.getJSONArray("list");
				//ArrayList<VendorBean> vendors = new ArrayList<VendorBean>();				
				JSONObject vendorObject = array.getJSONObject(0);
			
				String vendorType = "";
				try {
					vendorType = (String) vendorObject.get("vendorType");
				}catch(Exception e) {
					
				}
				String vendorSiteName = "";
				try {
					vendorSiteName = (String) vendorObject.get("vendorSiteName");
				}catch(Exception e) {
					
				}
				String vendorAccountName = "";
				try {
					vendorAccountName = (String) vendorObject.get("vendorAccountName");
				}catch(Exception e) {
					
				}
				String vendorId = "";
				try {
					vendorId = (String) vendorObject.get("vendorId");
				}catch(Exception e) {
					
				}
				String bankName = "";
				try {
					bankName = (String) vendorObject.get("bankName");
				}catch(Exception e) {
					
				}
				String defaultAccount = "";
				try {
					defaultAccount = (String) vendorObject.get("defaultAccount");
				}catch(Exception e) {
					
				}
				String vendorName = "";
				try {
					vendorName = (String) vendorObject.get("vendorName");
				}catch(Exception e) {
					
				}
				String employeeNo = "";
				try {
					employeeNo = (String) vendorObject.get("employeeNo");
				}catch(Exception e) {
					
				}
				String vendorCode = "";
				try {
					vendorCode = (String) vendorObject.get("vendorCode");
				}catch(Exception e) {
					
				}
				String failure = "";
				try {
					failure = (String) vendorObject.get("failure");
				}catch(Exception e) {
					
				}
				String vendorAccountNo = "";
				try {
					vendorAccountNo = (String) vendorObject.get("vendorAccountNo");
				}catch(Exception e) {
					
				}
				String id = "";
				try {
					id = (String) vendorObject.get("id");
				}catch(Exception e) {
					
				}
				vendor = new VendorBean(vendorType,  vendorSiteName,  vendorAccountName,  vendorId,
						bankName,  defaultAccount,  vendorName,  employeeNo,  vendorCode,
						failure,  vendorAccountNo,  id);
				}catch(JSONException e){
					e.getStackTrace();
				}
				
			}
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    	
    	logger.info(" 查询员工供应商列表 ------- end -------------------");
	}
	
	public void paymentVendor() {
	  String params = "{\"page\":{\"currentPage\":1,\"pageSize\":10,\"recordCount\":0},\"params\":{\"buCode\":\"B29\",\"vendorName\":\"\",\"bankName\":\"\"},\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
	  logger.info(" 查询员工供应商列表 ------- start -------------------");
      String url =Constants.FIND_PAGED_VENDOR_LIST_URL;
    
      try {
          result = HttpHelper.post(params, url);      
          logger.info("findPagedVendorList ---" + result);
          JSONObject object = JSONUtil.toJSON(result);
          int total = object.getInt("total");
          if(total>0) {
              try {
              JSONArray array = object.getJSONArray("list");
              //ArrayList<VendorBean> vendors = new ArrayList<VendorBean>();              
              JSONObject vendorObject = array.getJSONObject(0);
          
              String vendorType = "";
              try {
                  vendorType = (String) vendorObject.get("vendorType");
              }catch(Exception e) {
                  
              }
              String vendorSiteName = "";
              try {
                  vendorSiteName = (String) vendorObject.get("vendorSiteName");
              }catch(Exception e) {
                  
              }
              String vendorAccountName = "";
              try {
                  vendorAccountName = (String) vendorObject.get("vendorAccountName");
              }catch(Exception e) {
                  
              }
              String vendorId = "";
              try {
                  vendorId = (String) vendorObject.get("vendorId");
              }catch(Exception e) {
                  
              }
              String bankName = "";
              try {
                  bankName = (String) vendorObject.get("bankName");
              }catch(Exception e) {
                  
              }
              String defaultAccount = "";
              try {
                  defaultAccount = (String) vendorObject.get("defaultAccount");
              }catch(Exception e) {
                  
              }
              String vendorName = "";
              try {
                  vendorName = (String) vendorObject.get("vendorName");
              }catch(Exception e) {
                  
              }
              String employeeNo = "";
              try {
                  employeeNo = (String) vendorObject.get("employeeNo");
              }catch(Exception e) {
                  
              }
              String vendorCode = "";
              try {
                  vendorCode = (String) vendorObject.get("vendorCode");
              }catch(Exception e) {
                  
              }
              String failure = "";
              try {
                  failure = (String) vendorObject.get("failure");
              }catch(Exception e) {
                  
              }
              String vendorAccountNo = "";
              try {
                  vendorAccountNo = (String) vendorObject.get("vendorAccountNo");
              }catch(Exception e) {
                  
              }
              String id = "";
              try {
                  id = (String) vendorObject.get("id");
              }catch(Exception e) {
                  
              }
              vendor = new VendorBean(vendorType,  vendorSiteName,  vendorAccountName,  vendorId,
                      bankName,  defaultAccount,  vendorName,  employeeNo,  vendorCode,
                      failure,  vendorAccountNo,  id);
              }catch(JSONException e){
                  e.getStackTrace();
              }
              
          }
      }catch(Exception e) {
          e.printStackTrace();
      }
      
      logger.info(" 查询员工供应商列表 ------- end -------------------");
	}
	
	public EmployeeBankAccount findForEmployee(String empNo) {
	  logger.info("findForEmployee -" + empNo+"--------");
	  EmployeeBankAccount account = null;
	  String url = Constants.FIND_FOR_EMPLOYEE_URL + "/"+empNo;
	  try {
	    String params  = "{\"id\":\""+empNo+"\"}";
	    result = HttpHelper.post(params,url);
	    status = HttpHelper.isOk;
	    if(status) {
	      try {
	        account =  JSONToBean.parseJson2Object(EmployeeBankAccount.class, result);
	        logger.info(account.toString());
	      }catch(JSONException e) {
	        remark = "JSON 解析错误";
	      }
	    }else {
	      try {
	        JSONObject object = new JSONObject(result);
	        remark = object.getString("message");
	      }catch(JSONException e) {
	        remark = "JSON 解析错误";
	      }
	    }
	  }catch(Exception e) {
	    logger.error("系统异常");
	    status = false;
	    remark = "系统异常";
	  }
	  return account;
	}
}
